package com.wangzhixuan.apiController;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.base.ApiBaseController;
import com.wangzhixuan.commons.constant.ApiCode;
import com.wangzhixuan.commons.scan.MyException;
import com.wangzhixuan.model.Category;
import com.wangzhixuan.model.vo.CategoryVo;
import com.wangzhixuan.service.ICategoryService;

/**
 * <p>
 *   前端控制器
 * </p>
 * @author zhixuan.wang
 * @since 2017-07-15
 */
@Controller
@RequestMapping("/api/workLibrary")
public class CategoryApiController extends ApiBaseController {
    
    @Autowired private ICategoryService categoryService;

    /**
     * 添加
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/addCategory")
    @ResponseBody
    public Object addCategory(CategoryVo categoryVo) throws IllegalAccessException, InvocationTargetException, MyException {
    	categoryVo.checekParam();
    	Category category = new Category();
    	BeanUtils.copyProperties(category,categoryVo);
    	category.setJobDescribe(categoryVo.getJobDescrib());
    	category.setCreateId(categoryVo.getUserId());
    	category.setCreateTime(new Date());
        boolean b = categoryService.insert(category);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "添加失败");
        }
    }

    /**
     * 编辑
     * @param 
     * @return
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws MyException 
     */
    @RequestMapping("/editCategory")
    @ResponseBody
    public Object editCategory(CategoryVo categoryVo) throws IllegalAccessException, InvocationTargetException, MyException {
		if(categoryVo.getJobId()==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	categoryVo.checekParam();
    	Category category = new Category();
    	BeanUtils.copyProperties(category,categoryVo);
    	category.setJobDescribe(categoryVo.getJobDescrib());
    	category.setId(categoryVo.getJobId());
        boolean b = categoryService.updateById(category);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "编辑失败");
        }
    }
    
    @RequestMapping("/listCategory")
    @ResponseBody
    public Object listCategory() throws MyException {
    	List<Map<String,Object>> retList = categoryService.selectList();	
        return renderSuccess(ApiCode.CODE_200, "成功",retList);
    }
    
    /**
     * 删除
     * @param id
     * @return
     * @throws MyException 
     */
    @RequestMapping("/deleteCategory")
    @ResponseBody
    public Object deleteCategory(Integer jobId,Integer userId) throws MyException {
    	if(jobId==null){
			 throw new MyException(ApiCode.CODE_501, "网络异常，请稍后重试");
		}
    	if(userId==null){
			 throw new MyException(ApiCode.CODE_501, "请重新登录");
		}
        boolean b = categoryService.deleteById(jobId);
        if (b) {
        	return renderSuccess(ApiCode.CODE_200, "成功");
        } else {
        	return renderError(ApiCode.CODE_500, "删除失败");
        }
    }
    
    
    
    @RequestMapping("/editJobDescrib")
    @ResponseBody
    public Object editJobDescrib(Integer jobId,String describContent) throws MyException {
    	Category category = categoryService.selectById(jobId);
    	category.setJobDescribe(describContent);
    	boolean b = categoryService.updateById(category);
    	if(b){
            return renderSuccess(ApiCode.CODE_200, "更新成功");
    	}else{
    		return renderError(ApiCode.CODE_500, "更新失败");
    	}

    }
}
